package client;

import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import javax.swing.*;
import Server.Song;
import Server.SongDTO;

public class TralalaGui extends JFrame{

        private static final long serialVersionUID = 1L;
        private TralalaController controller;
        
        JPanel left;
        JPanel left1;
        JPanel left2;
        JPanel right;
        JPanel right1;
        JPanel right2;
        JPanel info;
        JPanel buttons;
        
        JTextField search;
        
        JLabel title1;
        JLabel title2;
        JLabel artist1;
        JLabel artist2;
        JLabel duration1;
        JLabel duration2;
        JLabel status1;
        JLabel status2;
        
        JButton play;
        JButton rewind;
        JButton forward;
        JButton stop;
        JButton search2;
        JButton searchall;
        
        JTextArea lyric;
        JScrollPane lyrics;
        
        public TralalaGui(TralalaController controller)
        {
        this.controller = controller;
        
        left = new JPanel();
        left1 = new JPanel();
        left2 = new JPanel();
        right = new JPanel();
        right1 = new JPanel();
        right2 = new JPanel();
        info = new JPanel();
        buttons = new JPanel();
        
        search = new JTextField("");
        
        title1 = new JLabel("   Title: ");
        title2 = new JLabel();
        artist1 = new JLabel("   Artist: ");
        artist2 = new JLabel();
        duration1 = new JLabel("   Duration: ");
        duration2 = new JLabel();
        status1 = new JLabel("   Status: ");
        status2 = new JLabel("");
        
        play = new JButton("Play");
        rewind = new JButton("Rewind");
        forward = new JButton("Forward");
        stop = new JButton("Stop");
        search2 = new JButton("Search");
        searchall = new JButton("Search all");
        
        lyric = new JTextArea();
        lyrics = new JScrollPane(lyric, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        right1.setLayout(new GridLayout(1, 3));
        right1.add(search);
        right1.add(search2);
        right1.add(searchall);
        
        right2.setLayout(new BorderLayout());
        right2.add(lyrics, "Center");
        
        right.setLayout(new BorderLayout());
        right.add(right1, "North");
        right.add(right2, "Center");
        
        buttons.setLayout(new GridLayout(2, 2));
        buttons.add(rewind);
        buttons.add(play);
        buttons.add(forward);
        buttons.add(stop);
        
        left1.setLayout(new GridLayout(4, 2));
        left1.add(title1);
        left1.add(title2);
        left1.add(artist1);
        left1.add(artist2);
        left1.add(duration1);
        left1.add(duration2);
        left1.add(status1);
        left1.add(status2);
        
        left2.add(buttons);
        
        left.setLayout(new GridLayout(2, 1));
        left.add(left1);
        left.add(left2);
        
        this.setLayout(new GridLayout(1, 2));
        this.add(left);
        this.add(right);
        
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(500, 200);
        
        play.addActionListener(new escuchador());
        stop.addActionListener(new escuchador());
        search2.addActionListener(new escuchador());
        searchall.addActionListener(new escuchador());
        this.setVisible(true);
        this.pack();
        }
        
        class escuchador implements ActionListener
        {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                        // TODO Auto-generated method stub
                        if(e.getActionCommand() == "Play")
                        {
                                play.setText("Pause");
                                status2.setText("Playing");
                        }
                        else
                        {
                                if(e.getActionCommand() == "Pause")
                                {
                                        play.setText("Play");
                                        status2.setText("Paused");
                                }
                                else
                                {
                                        if(e.getActionCommand() == "Stop")
                                        {
                                                status2.setText("Stopped");
                                                play.setText("Play");
                                        }
                                        else
                                        {
                                                if(e.getActionCommand() == "Search")
                                                {
                                                        if(search.getText() != "")
                                                        {
                                                                System.out.println(search.getText());
                                                                Song s = null;
                                                                try
                                                                {		
                                                                		System.out.println("Llego al cliente");
                                                                        s = controller.receiveASong(search.getText());
                                                                        System.out.println("Llego al cliente y solicito con exito");
                                                                }
                                                                catch (RemoteException e1)
                                                                {
                                                                		System.out.println("Llego al cliente y no solicito con exito");
                                                                        e1.printStackTrace();
                                                                }
                                                                title2.setText(s.getTitulo());
                                                                artist2.setText(s.getArtist());
                                                                duration2.setText("" + s.getLenght());
                                                                status2.setText("Playing");
                                                                lyric.setText(s.getLyrics());
                                                                play.setText("Pause");
                                                        }
                                                }
                                                else
                                                {
                                                	if(e.getActionCommand() == "Search all")
                                                	{
                                                		SongDTO[] s = null;
                                                		try
                                                        {	
                                                				System.out.println("Llego al cliente");
                                                                s = controller.receiveSongs();
                                                                System.out.println("Llego al cliente y solicito con exito");

                                                        }
                                                        catch (RemoteException e1)
                                                        {
                                                    			System.out.println("Llego al cliente y no solicito con exito");
                                                                e1.printStackTrace();
                                                        }
                                                        Songs v = new Songs(s);
                                                        v.setVisible(true);
                                                	}
                                                }
                                        }
                                }
                        }
                }
                
        }
        
        public static void main (String[] args)
        {
                TralalaGui t = new TralalaGui(null);
                t.setVisible(true);
        }
}